/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.controller.setter;

import foxmob.mlts.component.FileManager;
import foxmob.mlts.model.QuestionPaper;
import foxmob.mlts.model.Section;
import foxmob.mlts.model.Subsection;
import foxmob.mlts.model.TreeObject;
import foxmob.mlts.viewcontroller.setter.QuestionForm;
import foxmob.mlts.viewcontroller.setter.SubsectionForm;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Lunayo
 */
public class SubsectionFormController extends RootModifyFormController {
    
    private static final String EDIT_LABEL = "Save Subsection";
    private static final String ADD_LABEL = "Add Subsection";
    
    private SubsectionForm delegate;
    
    public SubsectionFormController(SetterFormController setterFormDelegate, 
            SubsectionForm delegate) {
        super(setterFormDelegate);
        this.delegate = delegate;
    }
    
    public void setDelegate(SubsectionForm delegate) {
        this.delegate = delegate;
    }
    
    public SubsectionForm getDelegate() {
        return delegate;
    }
    
    public void fillSubsectionComponents(TreeObject obj) {
        delegate.getDescriptionText().setText(obj.getSubsection().getDescription());
        delegate.getSubsectionText().setText(obj.getSubsection().getTitle());
    }
    
    public void eventChanged(SetterFormController.ActionEvent status) {
        if (this.currentState == SetterFormController.ActionEvent.ADD_EVENT) {
            delegate.getSaveButton().setText(ADD_LABEL);
            delegate.getAddQuestionButton().setVisible(false);
            delegate.getDeleteQuestionButton().setVisible(false);
        } else if (this.currentState == SetterFormController.ActionEvent.EDIT_EVENT) {
            delegate.getSaveButton().setText(EDIT_LABEL);
            delegate.getAddQuestionButton().setVisible(true);
            delegate.getDeleteQuestionButton().setVisible(true);
        } 
    }
    
    
    @Override
    public void setCurrentState(SetterFormController.ActionEvent state) {
        super.setCurrentState(state);
        eventChanged(state);
    }
    
    
    @Override
    public boolean validateInputComponent() {
        super.validateInputComponent();
        String message =  "Please input all of the information";
        if (delegate.getSubsectionText().getText().equals("") ||
                delegate.getSubsectionText().getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, message);
            delegate.getSubsectionText().requestFocus();
            return false;
        } 
        return true;
    }
    
    @Override
    public void resetInputComponent() {
        super.resetInputComponent();
        delegate.getDescriptionText().setText("");
        delegate.getSubsectionText().setText("");
    }
    
    private Subsection getSubsectionComponent() {
        Subsection subsection = new Subsection();
        
        subsection.setDescription(delegate.getDescriptionText().getText());
        subsection.setTitle(delegate.getSubsectionText().getText());
        
        return subsection;
    }
    
    private void editSubsectionFromRecord(QuestionPaper record) {
        if (!validateInputComponent()) {
            return;
        } 
        try {
            // TODO add your handling code here:
            FileManager manager = FileManager.getInstanceObject();
            Section section =
                record.getSectionWithID(treeObject.getSection().getID());
            Subsection subsection =
                section.getSubsectionWithID(treeObject.getSubsection().getID());
            Subsection componentSubsection = getSubsectionComponent();
            subsection.setDescription(componentSubsection.getDescription());
            subsection.setTitle(componentSubsection.getTitle());
            section.modifySubSection(subsection);
            // Save to file
            manager.saveToFile(record);
            String st ="Subsection saved successfully!";
            JOptionPane.showMessageDialog(null,st);
        } catch (IOException ex) {
            Logger.getLogger(QuestionForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void addSubsectionToRecord(QuestionPaper record) {
        FileManager manager = FileManager.getInstanceObject();
        Section section =
            record.getSectionWithID(treeObject.getSection().getID());
        if (!validateInputComponent()) {
            return;
        } else if (section.isExistSubsection(getSubsectionComponent())) {
            // display message box
            String message = "Record existed, please choose another name";
            JOptionPane.showMessageDialog(null, message);
            return;
        }
        try {
            // TODO add your handling code here:
            section.addSubsection(getSubsectionComponent());
            // Save to file
            manager.saveToFile(record);
            String st ="Subsection added successfully!";
            JOptionPane.showMessageDialog(null,st);
        } catch (IOException ex) {
            Logger.getLogger(QuestionForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /*
     * Listener
     */
    
    public void saveButtonDidTap() {
        if (!validateInputComponent()) return;
        if (this.currentState == SetterFormController.ActionEvent.ADD_EVENT) {
            addSubsectionToRecord(treeObject.getRecord());
            // notify the form the process has finished
            try {
                setterFormDelegate.addTreeDidFinish(getSubsectionComponent(),
                                            treeObject.getParentNode());
            } catch (Exception e) {
                System.err.print("Error" + e.getMessage());
            }
            // reset the component
            resetInputComponent();
        } else if (this.currentState == SetterFormController.ActionEvent.EDIT_EVENT) {
            editSubsectionFromRecord(treeObject.getRecord());
            // notify the form the process has finished
            try {
                setterFormDelegate.editTreeDidFinish(treeObject.getSubsection(),
                                            treeObject.getParentNode());
            } catch (Exception e) {
                System.err.print("Error" + e.getMessage());
            }
        }
    }
    
    public void deleteQuestionButtonDidTap() {
        try {
            setterFormDelegate.invokeComponent(SetterFormController.ActionEvent.DELETE_EVENT);
        } catch (Exception e) {
            System.err.print("Error : " + e.getMessage());
        }
        // reset the component
        resetInputComponent();
    }
    
    public void addQuestionButtonDidTap() {
        try {
            setterFormDelegate.invokeComponent(SetterFormController.ActionEvent.ADD_EVENT);
        } catch (Exception e) {
            System.err.print("Error : " + e.getMessage());
        }
    }
}
